package com.mocoop.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

import com.mocoop.entity.PageInfoEntity;
import com.mocoop.ui.MocoopFragment;

public class MocoopSingletonActivity extends MocoopActivity {
	protected RelativeLayout fragmentsLayout;
	protected List<PageInfoEntity> history = new ArrayList<PageInfoEntity>();
	public boolean isJumpEnd = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		currentFragment = new MocoopFragment();
		currentFragment.setCordovaWebView(appView);
		currentFragment.setLeftLayoutEvent(getLeftLayoutEvent());
		getFragmentManager().beginTransaction().add(fragmentsLayout.getId(), currentFragment).addToBackStack(null).commitAllowingStateLoss();
	}
	
	

	public void initView() {
		setContentView(R.layout.activity_singleton);
		fragmentsLayout = (RelativeLayout) findViewById(R.id.fragmentsLayout);
	}

	private synchronized void executeBack(final MocoopFragment.MyAnimationCallBack animationCallBack) {

		Animation animation = AnimationUtils.loadAnimation(this, R.anim.my_slide_out_from_left);
		MocoopFragment.MyAnimationLisener myAnimationLisener = currentFragment.new MyAnimationLisener(new MocoopFragment.MyAnimationCallBack() {
			@Override
			public void onEnd() {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						currentFragment.showLoadView(null);
						currentFragment.setPageInfo(history.get(history.size() - 1));

						if (animationCallBack != null) {
							animationCallBack.onEnd();
						}
					}
				});
			}
		});
		animation.setAnimationListener(myAnimationLisener);
		currentFragment.getView().startAnimation(animation);

	}

	@Override
	public synchronized void jumpNewPage(final PageInfoEntity pageInfoEntity) {
		if (isJumpEnd ) {
			isJumpEnd = false;
			if (history.size() == 0) {
				history.add(currentFragment.getPageInfoEntity());
			} else if (history.size() == 1) {
				history.set(0, currentFragment.getPageInfoEntity());
			}

			history.add(pageInfoEntity);

			if (pageInfoEntity.getAnimated()) {
				Animation animation = AnimationUtils.loadAnimation(this, R.anim.my_slide_in_from_left);
				currentFragment.showLoadView(animation);
				currentFragment.getTitleBar().startAnimation(animation);
			}
			pageInfoEntity.setAnimated(false);
			currentFragment.setPageInfo(pageInfoEntity);
			String url; 
			if(pageInfoEntity.getUrl().startsWith("http")){
				url = pageInfoEntity.getUrl();
			}else{
				url = getBASE_URL()+pageInfoEntity.getUrl();
			}
			currentFragment.loadURL(url);
		}
	}

	@Override
	public synchronized void goBack(final MocoopFragment.MyAnimationCallBack animationCallBack) {
		if (isJumpEnd) {
			isJumpEnd = false;
			if (!appView.canGoBack()) {
				dialog();
			} else {
				history.remove(history.size() - 1);
				executeBack(animationCallBack);
				currentFragment.getCordovaWebView().backHistory();
			}
		}
	}

	@Override
	public synchronized void showMeRightNow() {
		currentFragment.hidePromptView();
		currentFragment.hideLoadView(AnimationUtils.loadAnimation(this, R.anim.my_alpha_out));
	}

	@Override
	public Object onMessage(String id, Object data) {
		super.onMessage(id, data);
		if ("onPageFinished".equals(id)) {
			isJumpEnd = true;
		}
		return null;
	}

	@Override
	public void onButtonBack() {
		goBack(null);
	}

	@Override
	public void onButtonMenu() {

	}

	@Override
	public synchronized boolean backToSpecifyPage(String targetURL) {
		boolean isExist = false;
		if (!appView.canGoBack()) {
			dialog();
			return isExist;
		}

		int count = 0;
		for (int index = history.size() - 1; index > 0; index--) {
			PageInfoEntity infoEntity = history.get(index);
			if (infoEntity.getUrl().equals(targetURL)) {
				isExist = true;
				break;
			} else {
				count--;
				history.remove(infoEntity);
			}
		}

		executeBack(null);
		currentFragment.getCordovaWebView().loadUrl("javascript:window.history.go(" + count + ");");
		return isExist;
	}

	@Override
	public void notificationToUI(final String js, final String noticePages) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				boolean flag = false;
				String currentURL = currentFragment.getCordovaWebView().getUrl();
				if ("ALL".equals(noticePages)) {
					flag = true;
				} else {
					JSONArray jsonarray;
					try {
						jsonarray = new JSONArray(noticePages);

						for (int i = 0; i < jsonarray.length(); i++) {
							if (currentURL.indexOf(jsonarray.getString(i)) >= 0) {
								flag = true;
							}
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				if (flag) {
					loadUrl(js);
				}
			}
		});
	}



	@Override
	public List<MocoopFragment> getHistoryList() {
		List<MocoopFragment> arrayList = new ArrayList<MocoopFragment>();
		arrayList.add(currentFragment);
		return arrayList;
	}

}
